package cn.com.winning.ecare.activity;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.winning.ecare.common.BtoECalendarDialog;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.common.MyTimeUtil;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.UserRecord;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**检查检验报告主页面
 * @author Administrator
 *
 */
public class ReportActivity extends TabActivity {

	TabHost tabHost;
	private TabWidget mTabWidget;
	private ReportActivity othis = this;
	Grideviewmenu gm;
	private GridView gridView;
	CustomProgressDialog proDialog;// 加载框
	/**
	 * 侧滑布局对象，用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
	 */
//	private SlidingLayout slidingLayout;
	
	private LinearLayout content;

	/**
	 * menu按钮，点击按钮展示左侧布局，再点击一次隐藏左侧布局。
	 */
	private ImageButton menuButton;
	
	private ImageButton btnSearch;//日期选择按钮
	private LinearLayout layout;
	private int height, statusBarHeight;
	private String startDate="",endDate="";
	MyApplication myApp;
	private TextView startDateText, endDateText,updateTime;
	private int tab = 0;// 记录是哪个tab，默认是检查报告为0，检验报告tab为1
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_report_main);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		myApp.getUserRecords().setUsername(myApp.getAccount());
		myApp.getUserRecords().setModule("ReportActivity@检查检验报告");
		UserRecord.getInstance(myApp.getUserRecords()).init();
		
		tabHost = getTabHost();
		mTabWidget = (TabWidget) this.findViewById(android.R.id.tabs);
		
		startDateText = (TextView) this.findViewById(R.id.startDateText);
		endDateText = (TextView) this.findViewById(R.id.endDateText);
		updateTime = (TextView) this.findViewById(R.id.updateTime);
//		updateTime.setText("服务器最新数据同步于:"
//				+ MyTimeUtil.GetSysDate(MyTimeUtil.yyyy_MM_dd_, MyTimeUtil.DateToStr(MyTimeUtil.yyyy_MM_dd_, new Date()), 0, 0, -14));
		
		// 主菜单配置信息传递到下一页面
		
		//接受参数，主要用于日期选择后的日期获取，然后再传入每个tab
		Intent intent = getIntent();
		String startDate1 = intent.getStringExtra("startDate");
		String endDate1 = intent.getStringExtra("endDate");
		if((startDate1==null||"".equals(startDate1))&&(endDate1==null||"".equals(endDate1))){
			//初次打开，没有选择日期
			Date now = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			startDate = simpleDateFormat.format(now);//默认当天
			endDate = simpleDateFormat.format(now);
		}else{
			//选择的日期赋值
			startDate = startDate1;
			endDate = endDate1;
			tab = Integer.parseInt(intent.getStringExtra("tab"));
		}
		//设置初始化时间
		startDateText.setText(startDate);
		endDateText.setText(endDate);
		
		// ////////////////处理tabhost加载选定页面，始终先加载第一个再跳入选定页面问题
		Field mCurrentTab = null;
		try
		{
			mCurrentTab = tabHost.getClass().getDeclaredField("mCurrentTab");
			mCurrentTab.setAccessible(true);
			mCurrentTab.setInt(tabHost, -2);
		}
		catch (SecurityException e)
		{
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		catch (NoSuchFieldException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		// //////////////////////////////////////
		
		Intent intent1 = new Intent();
		intent1.putExtra("startDate", startDate);
		intent1.putExtra("endDate", endDate);
		intent1.setClass(this, CheckUpActivity.class);
		Intent intent2 = new Intent();
		intent2.putExtra("startDate", startDate);
		intent2.putExtra("endDate", endDate);
		intent2.setClass(this, CheckOutActivity.class);


		tabHost.addTab(tabHost.newTabSpec("checkupreport").setIndicator("检查报告")
				.setContent(intent1));
		tabHost.addTab(tabHost.newTabSpec("checkoutreport")
				.setIndicator("检验报告")
				.setContent(intent2));
		
		// //////////////
		try
		{
			if (tab == 0)
			{
				mCurrentTab.setInt(tabHost, 1);
			}
			else
			{
				mCurrentTab.setInt(tabHost, 0);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		// ///////////////////////
		tabHost.setCurrentTab(tab);// 设置初始选中状态为第一个tab
		
		iniTabHost();
	
		menuButton = (ImageButton) findViewById(R.id.menuButton);

		menuButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				othis.onBackPressed();
			}
		});

		btnSearch = (ImageButton) this.findViewById(R.id.searchButton);
		layout = (LinearLayout) this.findViewById(R.id.jclayout);
		// 设置监听
		setListener();
		
		
	}

	private void iniTabHost() {
		// 初始化TabHost
		for (int i = 0; i < mTabWidget.getChildCount(); i++) {
			RelativeLayout view = (RelativeLayout) mTabWidget.getChildAt(i);

			TextView text = (TextView) view.findViewById(android.R.id.title);
			text.setTextSize(18);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text
					.getLayoutParams();
			params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
			params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
			text.setLayoutParams(params);
			text.setGravity(Gravity.CENTER);
			view.setBackgroundColor(getResources().getColor(R.color.color_white));
			if (tabHost.getCurrentTab() == i) {
				text.setTextColor(getResources().getColor(R.color.listheadtext_color));
			} else {
				text.setTextColor(getResources().getColor(R.color.test_color_register));
			}
			int heightPx= DisplayUtil.dip2px(this, 35);
			mTabWidget.getChildAt(i).getLayoutParams().height = heightPx;
		}
		// TabHost切换
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				for (int i = 0; i < mTabWidget.getChildCount(); i++) {
					View view = mTabWidget.getChildAt(i);
					TextView text = (TextView) view.findViewById(android.R.id.title);
					if (tabHost.getCurrentTab() == i) {
						text.setTextColor(getResources().getColor(R.color.listheadtext_color));
					} else {
						text.setTextColor(getResources().getColor(R.color.test_color_register));
					}
					tab = tabHost.getCurrentTab();
					
				}
			}

		});
	}
	
	private void setListener() {
		btnSearch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				initDateDialog();

			}
		});
		startDateText.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				initDateDialog();
			}
		});

		endDateText.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				initDateDialog();
			}
		});

	}
	
	private void initDateDialog(){
		// 初始化一个自定义的Dialog
		final BtoECalendarDialog dialog = new BtoECalendarDialog(
				ReportActivity.this, R.style.MyDialog,startDateText.getText().toString(),endDateText.getText().toString());
		// 获取状态栏高度
		Rect frame = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		statusBarHeight = frame.top;
		Log.i("aaa", "statusBarHeight :" + statusBarHeight);
		// 获取layout高度
		height = layout.getHeight();
		dialog.show();
		Window mWindow = dialog.getWindow();
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.x = 0;
		lp.y = height + statusBarHeight;
		lp.width = (int) (display.getWidth()); // 设置宽度
		lp.height = (int) (display.getHeight()) - height
				- statusBarHeight;// 设置高度
		mWindow.setAttributes(lp);

		Button btnok = (Button) dialog.findViewById(R.id.btnok);
		Button btncancel = (Button) dialog.findViewById(R.id.btncancel);

		btnok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 选择开始日期
				TextView startDate = (TextView) dialog
						.findViewById(R.id.tvSelectedstart);
				// 选择结束日期
				TextView endDate = (TextView) dialog
						.findViewById(R.id.tvSelectedend);
				String sd = startDate.getText().toString();
				String ed = endDate.getText().toString();
				
				dialog.dismiss();
//				proDialog = CustomProgressDialog.createDialog(othis);
//				proDialog.setMessage("数据加载中...");
//				proDialog.show();
//				proDialog.setCanceledOnTouchOutside(false);
				
				//传入选择的日期，重新打开ReportActivity(要同时给tabhost中的两个tab页传入参数，暂无好方法，暂重刷主页面，同时传参)
				Intent intent = getIntent();
				intent.putExtra("startDate", sd);
				intent.putExtra("endDate", ed);
				intent.putExtra("tab", tab+"");
				intent.setClass(othis, ReportActivity.class);
				startActivity(intent);
				othis.finish();
			
			}
		});

		btncancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
	}
	
}
